home *** CD-ROM | disk | FTP | other *** search
/ Programming Sound Cards / Programming Sound Cards.iso / sound_45 / misc.c < prev    next >
C/C++ Source or Header  |  1995-01-01  |  7KB  |  276 lines

  1. /* SOX COPYRIGHT *************************************************** */
  2. /*
  3.  * July 5, 1991
  4.  * Copyright 1991 Lance Norskog And Sundry Contributors
  5.  * This source code is freely redistributable and may be used for
  6.  * any purpose.  This copyright notice must be maintained. 
  7.  * Lance Norskog And Sundry Contributors are not responsible for 
  8.  * the consequences of using this software.
  9.  */
  10. /* END OF SOX COPYRIGHT ******************************************** */
  11.  
  12. /* portions copied from sox */
  13. /* modified by David Lai Jan 6 1993, lai%fastfood@daver.bungi.com */
  14.  
  15. /* BEGIN DAVID LAI COPYRIGHT ********************************************* */
  16. /*
  17. (C) Copyright David Lai 1993, 1994.  All rights reserved.
  18.  
  19. The source code is copyright by David Lai, however it is freely
  20. distributable and released for unrestricted use.  Users may copy or modify 
  21. this source code without charge, provided all copyright
  22. notices remain intact in all the source code.  Portions of the source code
  23. copyright by their respective copyright holders and are covered
  24. under different agreements, however the source code used has
  25. specifically been marked distributable royalty-free.
  26.  
  27. You can do whatever you want with it, even charge money for it, if
  28. you find a sucker willing to pay for it.  This is not shareware, the program
  29. is not crippled in any way, do not send money.  You can e-mail comments
  30. to the electronic mail address below, or fax to the fax number below.
  31.  
  32. If you add a feature thats useful, or do a new port, you can send
  33. the context diffs to the e-mail address below.  I may include it in
  34. subsequent releases.  Your code must specifically be marked
  35. freely distributable, I will not include code marked otherwise.
  36.  
  37. THE SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
  38. THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  39. PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  40.  
  41. The source code is provided with no support and without any obligation on
  42. the part of David Lai to assist in its use, correction,
  43. modification or enhancement.
  44.  
  45. DAVID LAI SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  46. INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
  47. OR ANY PART THEREOF.
  48.  
  49. In no event will David Lai be liable for any lost revenue
  50. or profits or other special, indirect and consequential damages, even if
  51. David Lai has been advised of the possibility of such damages.
  52.  
  53. David Lai
  54. 1370 McKendrie St
  55. San Jose, CA 95126
  56. fax: 408-241-4615
  57.  
  58. lai%fastfood@daver.bungi.com
  59.  
  60. */
  61. /* END   DAVID LAI COPYRIGHT ********************************************* */
  62.  
  63. #include "misc.h"
  64. #include "modcomp.h"
  65.  
  66. static char readerr[] = "Premature EOF while reading file.";
  67. static char writerr[] = "Error writing file.  You are probably out of disk space.";
  68.  
  69. #define fail(x) err_exit(x,NULL)
  70.  
  71. /* Read short, little-endian: little end first. VAX/386 style. */
  72. uint16
  73. #ifdef  __STDC__
  74. rlshort(FILE * ft)
  75. #else
  76. rlshort(ft) FILE * ft;
  77. #endif
  78. {
  79.     unsigned char uc, uc2;
  80.     uc  = getc(ft);
  81.     uc2 = getc(ft);
  82.     if (ferror(ft)) fail(readerr);
  83.     return (uc2 << 8) | uc;
  84. }
  85.  
  86. /* Read short, bigendian: big first. 68000/SPARC style. */
  87. uint16
  88. #ifdef  __STDC__
  89. rbshort(FILE * ft)
  90. #else
  91. rbshort(ft) FILE * ft;
  92. #endif
  93. {
  94.     unsigned char uc, uc2;
  95.     uc2 = getc(ft);
  96.     uc  = getc(ft);
  97.     if (ferror(ft)) fail(readerr);
  98.     return (uc2 << 8) | uc;
  99. }
  100.  
  101. /* Write short, little-endian: little end first. VAX/386 style. */
  102. void
  103. #ifdef    __STDC__
  104. wlshort(FILE * ft, uint16 us)
  105. #else
  106. wlshort(ft, us)
  107. FILE * ft;
  108. uint16 us;
  109. #endif
  110. {
  111.     putc(us, ft);
  112.     putc(us >> 8, ft);
  113.     if (ferror(ft))
  114.         fail(writerr);
  115. }
  116.  
  117. /* Write short, big-endian: big end first. 68000/SPARC style. */
  118. void
  119. #ifdef    __STDC__
  120. wbshort(FILE * ft, uint16 us)
  121. #else
  122. wbshort(ft, us)
  123. FILE * ft;
  124. uint16 us;
  125. #endif
  126. {
  127.     putc(us >> 8, ft);
  128.     putc(us, ft);
  129.     if (ferror(ft))
  130.         fail(writerr);
  131. }
  132.  
  133. /* Read long, little-endian: little end first. VAX/386 style. */
  134. uint32
  135. #ifdef  __STDC__
  136. rllong(FILE * ft)
  137. #else
  138. rllong(ft) FILE * ft;
  139. #endif
  140. {
  141.     unsigned char uc, uc2, uc3, uc4;
  142. /*    if (feof(ft))
  143.         fail(readerr);        /* No worky! */
  144.     uc  = getc(ft);
  145.     uc2 = getc(ft);
  146.     uc3 = getc(ft);
  147.     uc4 = getc(ft);
  148.     if (ferror(ft)) fail(readerr);
  149.     return ((long)uc4 << 24) | ((long)uc3 << 16) | ((long)uc2 << 8) | (long)uc;
  150. }
  151.  
  152. /* Read long, bigendian: big first. 68000/SPARC style. */
  153. uint32
  154. #ifdef  __STDC__
  155. rblong(FILE * ft)
  156. #else
  157. rblong(ft) FILE * ft;
  158. #endif
  159. {
  160.     unsigned char uc, uc2, uc3, uc4;
  161. /*    if (feof(ft))
  162.         fail(readerr);        /* No worky! */
  163.     uc  = getc(ft);
  164.     uc2 = getc(ft);
  165.     uc3 = getc(ft);
  166.     uc4 = getc(ft);
  167.     if (ferror(ft)) fail(readerr);
  168.     return ((long)uc << 24) | ((long)uc2 << 16) | ((long)uc3 << 8) | (long)uc4;
  169. }
  170.  
  171. /* Write long, little-endian: little end first. VAX/386 style. */
  172. void
  173. #ifdef  __STDC__
  174. wllong(FILE * ft, uint32 ul)
  175. #else
  176. wllong(ft, ul) FILE * ft; uint32 ul;
  177. #endif
  178. {
  179. int datum;
  180.  
  181.     datum = (int)((ul) & 0xff);
  182.     putc(datum, ft);
  183.     datum = (int)((ul >> 8) & 0xff);
  184.     putc(datum, ft);
  185.     datum = (int)((ul >> 16) & 0xff);
  186.     putc(datum, ft);
  187.     datum = (int)((ul >> 24) & 0xff);
  188.     putc(datum, ft);
  189.     if (ferror(ft))
  190.         fail(writerr);
  191. }
  192.  
  193. /* Write long, big-endian: big end first. 68000/SPARC style. */
  194. void
  195. #ifdef  __STDC__
  196. wblong(FILE * ft, uint32 ul)
  197. #else
  198. wblong(ft, ul) FILE * ft; uint32 ul;
  199. #endif
  200. {
  201. int datum;
  202.  
  203.     datum = (int)((ul >> 24) & 0xff);
  204.     putc(datum, ft);
  205.     datum = (int)((ul >> 16) & 0xff);
  206.     putc(datum, ft);
  207.     datum = (int)((ul >> 8) & 0xff);
  208.     putc(datum, ft);
  209.     datum = (int)((ul) & 0xff);
  210.     putc(datum, ft);
  211.     if (ferror(ft))
  212.         fail(writerr);
  213. }
  214.  
  215. void
  216. #ifdef  __STDC__
  217. wezstr(FILE * ft, char *str)
  218. #else
  219. wezstr(ft, str) FILE * ft; char *str;
  220. #endif
  221. { /* write a null terminated string, pad until even number of bytes written */
  222. int len;
  223. len = strlen(str)+1; /* include null byte */
  224.     if ((int)fwrite(str, 1, len, ft)!= len)
  225.         fail(writerr);
  226.     if (len % 2) putc(0,ft);
  227. }
  228.  
  229. #ifdef __MSDOS__
  230. /* need a fwrite and fread that can handle > 64K */
  231. uint32 FWRITE(const void * buffer, size_t size, uint32 count, FILE *stream)
  232.  {
  233.  uint32 i;
  234.  unsigned j;
  235.  unsigned incr = (unsigned)(65520 / size);
  236.  i = 0;
  237.  while(count - i > 0)
  238.   {
  239.   j= fwrite(buffer, size, (((count - i)) > incr? incr: (unsigned)(count - i)), stream);
  240.   if (j!= ((count - i)>incr?incr:(unsigned)(count-i)))
  241.     fail(writerr);
  242.   i+=j;
  243.   buffer = ((char *) buffer) + j*size;
  244.   }
  245.  return i;
  246.  }
  247.  
  248. uint32 FREAD(void * buffer, size_t size, uint32 count, FILE *stream)
  249.  {
  250.  uint32 i;
  251.  unsigned j;
  252.  unsigned incr = (unsigned)(65520 / size);
  253.  i = 0;
  254.  while(count - i > 0)
  255.   {
  256.   j= fread(buffer, size, (((count - i)) > incr? incr:(unsigned)(count - i)), stream);
  257.   if (j!= ((count - i)>incr?incr:(unsigned)(count-i)))
  258.     fail(readerr);
  259.   i+=j;
  260.   buffer = ((char *) buffer) + j*size;
  261.   }
  262.  return i;
  263.  }
  264.  
  265. #endif
  266.  
  267. static char rcs_id[]="$Id: misc.c,v 1.1 1994/03/19 09:21:31 dlai Exp $";
  268.  
  269. #if 0
  270. $Log: misc.c,v $
  271.  * Revision 1.1  1994/03/19  09:21:31  dlai
  272.  * Initial revision
  273.  *
  274.  
  275. #endif
  276.